package com.flower.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.flower.auth.common.entity.Result;
import com.flower.auth.convert.AuthUserDTOConverter;
import com.flower.auth.dto.AuthUserDTO;

import com.flower.domain.entity.AuthUserBO;
import com.flower.domain.service.AuthUserDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-16 18:44
 */
@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {


    @Resource
    private AuthUserDomainService authUserDomainService;

    @RequestMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO){

        Preconditions.checkArgument(authUserDTO.getUserName() != null, "用户名不能为空");

        AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBOCopy(authUserDTO);

        Boolean register = authUserDomainService.register(authUserBO);

        return Result.ok(register);
    }


    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO){


        AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBOCopy(authUserDTO);

        authUserDomainService.update(authUserBO);

        return Result.ok();
    }


    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO){


        AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBOCopy(authUserDTO);

        Boolean delete = authUserDomainService.delete(authUserBO);

        return Result.ok(delete);
    }


    @RequestMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO){


        AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBOCopy(authUserDTO);

        Boolean delete = authUserDomainService.delete(authUserBO);

        return Result.ok(delete);
    }




        // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
        @RequestMapping("doLogin")
        public SaResult doLogin(@RequestParam String validCode) {
            SaTokenInfo saTokenInfo = authUserDomainService.deledoLoginte(validCode);
            return SaResult.error("登录失败");
        }

        // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
        @RequestMapping("isLogin")
        public String isLogin() {
            return "当前会话是否登录：" + StpUtil.isLogin();
        }




    @RequestMapping("getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO){


        AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBOCopy(authUserDTO);

        AuthUserBO bo = authUserDomainService.getUserInfo(authUserBO);



        return Result.ok(AuthUserDTOConverter.INSTANCE.convertBOToDTO(bo));
    }



    @RequestMapping("logout")
    public Result<AuthUserDTO> logout(@RequestParam String userName){



        StpUtil.logout(userName);



        return Result.ok();
    }

}
